package com.shq.ex1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.util.Log;

import java.io.IOException;
import java.util.Calendar;

/**
 * Description
 * Created by sunhine on 2018/3/15.
 */

public class AlarmReceive extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                "alarm_record",Context.MODE_PRIVATE
        );
        String hour = String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));
        String time = sharedPreferences.getString(hour+":"+minute,null);
        if(time != null){
            try {
    			MediaPlayer mp = new MediaPlayer();
				mp.setDataSource(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
				mp.prepare();
            System.out.println("时间到了");
				mp.start();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
}
